#line 1 "D:/Archivio/mikroC/tric-newsensors/lib_bma180.c"
#line 8 "D:/Archivio/mikroC/tric-newsensors/lib_bma180.c"
extern int acc[3];

void BMA180_init() {
 unsigned short temp;


 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x0D);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x81 );
 temp = I2C1_Rd(0);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x0D);
 I2C1_Wr(temp | 0x10);
 I2C1_Stop();
 Delay_ms(50);


 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x50);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x81 );
 temp = I2C1_Rd(0);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x50);
 I2C1_Wr((temp & 0xF8) | 0x01);
 I2C1_Stop();
 Delay_ms(10);


 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x20);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x81 );
 temp = I2C1_Rd(0);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x20);
 I2C1_Wr(temp & 0x0F);
 I2C1_Stop();
 Delay_ms(10);


 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x35);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x81 );
 temp = I2C1_Rd(0);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x35);
 I2C1_Wr(((temp & 0xF0) | 0x04));
 I2C1_Stop();
 Delay_ms(10);
}

void BMA180_readAcc() {
 unsigned short cnt;
 unsigned short acc_tmp[6];

 I2C1_Start();
 I2C1_Wr( 0x80 );
 I2C1_Wr(0x02);
 I2C1_Stop();
 I2C1_Start();
 I2C1_Wr( 0x81 );
 for (cnt=0; cnt<6; cnt++) {
 char ack;
 ack = (cnt<5);
 acc_tmp[cnt] = I2C1_Rd(ack);
 }
 I2C1_Stop();

 acc[0] = acc_tmp[1];
 acc[0] = acc[0] << 8;
 acc[0] |= acc_tmp[0];
 acc[0] = acc[0] >> 2;
 acc[1] = acc_tmp[3];
 acc[1] = acc[1] << 8;
 acc[1] |= acc_tmp[2];
 acc[1] = acc[1] >> 2;
 acc[2] = acc_tmp[5];
 acc[2] = acc[2] << 8;
 acc[2] |= acc_tmp[4];
 acc[2] = acc[2] >> 2;
}
